#include <iostream>
#include <sstream>
#include <string>
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <ctype.h>
#include <stdio.h>


using namespace std;

string replaceSpaces(string str) {
	int found = str.find(" ");
	while (found != string::npos) {
		str.replace(found, 1, "+");
		found = str.find(" ");
	}
	return str;
}

void chatterCallback(const std_msgs::String msg)
{
	string str = msg.data;
	str = replaceSpaces(str);
	
	stringstream sstream;
	sstream << "curl -A \"Mozilla\" \"http://translate.google.com/translate_tts?tl=en&q=";
	sstream << str;
	sstream << "\" > say.mp3; mplayer say.mp3";
	
	system(sstream.str().c_str());
				
}


int main(int argc, char **argv)
{
	
  ros::init(argc, argv, "talker");


  ros::NodeHandle n;

  ros::Subscriber sub;
  sub = n.subscribe("talker", 1000, chatterCallback);
  

  ros::Rate loop_rate(10);


  while (ros::ok())
  {

    ros::spinOnce();

    loop_rate.sleep();

  }

  return 0;
}
